﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using QL_DTTN.DTO.DTO;
using QL_DTTN.TTKT.BUS;
namespace QL_DTTN.TTKT
{
    public partial class FrmQuanLyDiemThi : DevExpress.XtraEditors.XtraForm
    {
        List<PhongThi> lstPhong;
        DiemThi _diemThi;
        public FrmQuanLyDiemThi()
        {
            InitializeComponent();
            lstPhong = new List<PhongThi>();
        }
        private void FrmQuanLyDiemThi_Load(object sender, EventArgs e)
        {
            loadControl();
            cbxTinh_Load();
            cbxTinhTK_Load();
            gctPhong.DataSource = lstPhong;
            cbxTinhTK_SelectedIndexChanged(sender, e);
            Them_Load();
        }
        private void btnThemPhong_Click(object sender, EventArgs e)
        {
            if (txtPhong.Text.Trim() == "")
            {
                return;
            }
            PhongThi pt = new PhongThi();
            pt.TenPhongThi = txtPhong.Text.Trim();
            if (btnThem.Visible)
            {
                pt.MaDiemThi = _diemThi.MaDiemThi;
                PhongThi_Bus.Them(pt);
            }
            lstPhong.Add(pt);
            gvwPhong.RefreshData();
        }
        private void txtPhong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnThemPhong_Click(sender, e);
            }
        }
        private void cbxTinh_Load()
        {
            cbxTinh.DataSource = Tinh_Bus.dataTable();
            cbxTinh.DisplayMember = "tenTinh";
            cbxTinh.ValueMember = "maTinh";
        }
        private void cbxTinhTK_Load()
        {
            cbxTinhTK.DataSource = Tinh_Bus.dataTable();
            cbxTinhTK.DisplayMember = "tenTinh";
            cbxTinhTK.ValueMember = "maTinh";
        }
        private void loadControl()
        {
            iluTinh.DataSource = Tinh_Bus.dataTable();
            iluTinh.DisplayMember = "tenTinh";
            iluTinh.ValueMember = "maTinh";
        }
        private void Them()
        {
            DiemThi dt = new DiemThi();
            dt.DiaChi = txtDiaChi.Text.Trim();
            dt.MaTinh = Convert.ToInt16(cbxTinh.SelectedValue);
            dt.SoDienThoai = txtSoDT.Text.Trim();
            dt.TenDiemThi = txtTenDiaDiem.Text.Trim();
            if (DiemThi_Bus.Them(dt, lstPhong))
            {
                XtraMessageBox.Show("Thêm thành công");
                Them_Load();
            }
            else
            {
                XtraMessageBox.Show("Thêm thất bại");
            }
        }
        private void Sua()
        {
            _diemThi.DiaChi = txtDiaChi.Text.Trim();
            _diemThi.MaTinh = Convert.ToInt16(cbxTinh.SelectedValue);
            _diemThi.SoDienThoai = txtSoDT.Text.Trim();
            _diemThi.TenDiemThi = txtTenDiaDiem.Text.Trim();
            if (DiemThi_Bus.Sua(_diemThi))
            {
                XtraMessageBox.Show("Sửa thành công");
            }
            else
            {
                XtraMessageBox.Show("Sửa thất bại");
            }
        }
        private void Them_Load()
        {
            gbxThongTin.Text = "Thêm điểm thi:";
            txtDiaChi.Text = "";
            txtSoDT.Text = "";
            txtTenDiaDiem.Text = "";
            btnThem.Visible = false;
            lstPhong.RemoveRange(0, lstPhong.Count);
            gvwPhong.RefreshData();
        }
        private void Sua_Load()
        {
            gbxThongTin.Text = "Sửa thông tin điểm thi:";
            cbxTinh.SelectedValue = _diemThi.MaTinh;
            txtDiaChi.Text = _diemThi.DiaChi;
            txtSoDT.Text = _diemThi.SoDienThoai;
            txtTenDiaDiem.Text = _diemThi.TenDiemThi;
            btnThem.Visible = true;
            lstPhong.RemoveRange(0, lstPhong.Count);
            lstPhong.AddRange(PhongThi_Bus.getList(_diemThi.MaDiemThi));
            gvwPhong.RefreshData();
        }
        private void cbxTinhTK_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int ma = Convert.ToInt32(cbxTinhTK.SelectedValue);
                gctDiemThi.DataSource = DiemThi_Bus.dataTable(ma);
            }
            catch
            {
            }

        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (btnThem.Visible)
            {
                Sua_Load();
            }
            else
            {
                Them_Load();
            }
        }
        private void gctPhong_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && gvwPhong.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Bạn muốn xóa?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    if (btnThem.Visible)
                    {
                        PhongThi_Bus.Xoa(Convert.ToInt64(gvwPhong.GetRowCellValue(gvwPhong.FocusedRowHandle, gvwPhong.Columns["MaPhongThi"])));
                    }
                    lstPhong.RemoveAt(gvwPhong.FocusedRowHandle);
                    gvwPhong.RefreshData();
                }
            }
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (btnThem.Visible)
            {
                Sua();
            }
            else
            {
                Them();
            }
        }
        private void gvwDiemThi_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            GridView view = sender as GridView;
            _diemThi = new DiemThi();
            _diemThi.MaDiemThi = Convert.ToInt64(view.GetRowCellValue(e.FocusedRowHandle, view.Columns["maDiemThi"]));
            _diemThi = DiemThi_Bus.GetDiemThi(_diemThi.MaDiemThi);
            if (_diemThi == null)
            {
                return;
            }
            Sua_Load();
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            Them_Load();
        }
    }
}